const data = {
	assistant_id: "DYDYGNjEjf3P",
	user_id: "100037651362",
	stream: false,
	messages: [],

}
const header = {
	"X-Source": "openapi",
	"Content-Type": "application/json",
	"Authorization": "Bearer g3zN7vYQz7UczV5HldF9a0TmPPqpciUf"
}

async function changeModel(msg) {
	try {
		data.assistant_id = msg.roleId
		header.Authorization = `Bearer ${msg.token}`
		data.messages = []
		return true
	} catch (err) {
		return false
	}

}

async function agentText(msg) {
	return new Promise((resolve, reject) => {
		if (msg) {
			data.messages.push({
				role: "user",
				content: [{
					type: "text",
					text: msg
				}]
			})
		}
		uni.request({
			url: "https://open.hunyuan.tencent.com/openapi/v1/agent/chat/completions",
			method: 'POST',
			header: header,
			data: data,
			dataType: 'json',
			sslVerify: false,
			success: (response) => {
				if (response.data.choices) {
					data.messages.push({
						role: "assistant",
						content: [{
							type: "text",
							text: response.data.choices[0].message.content
						}]
					})
					resolve(response.data);
				} else {
					resolve(response.data);
					data.messages.pop()
				}
			},
			fail: (error) => {
				reject(error)
				console.error(error); // 请求失败的情况  
			}
		});
	})
}

export {
	agentText,
	changeModel
}